package com.autotest.nore.service;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.autotest.nore.dto.CaseApiDTO;
import com.autotest.nore.dto.CaseApiReportDTO;
import com.autotest.nore.mapper.CaseApiMapper;
import com.autotest.nore.dto.ApiCaseDTO;
import com.autotest.nore.request.ApiCaseRequest;
import com.autotest.nore.request.QueryRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.UUID;

@Service
public class CaseApiService {
    @Autowired
    private CaseApiMapper caseApiMapper;

    public void deleteById(String id){
        caseApiMapper.deleteById(id);
    }

    public List<ApiCaseDTO> getCaseApiListByApi(String apiId){

        return caseApiMapper.getCaseApiListByApi(apiId);
    }
    public List<ApiCaseDTO> getCaseApiList(QueryRequest request){
        return caseApiMapper.getCaseApiList2(request);

    }

    public String saveCaseApi(ApiCaseRequest apiCaseRequest){
        ApiCaseDTO apiCaseDTO=new ApiCaseDTO();
        String caseApiName=apiCaseRequest.getCaseapiName();
        String apiId=apiCaseRequest.getApiId();
        String caseApiDesc=apiCaseRequest.getCaseapiDesc();
        JSONArray caseApiHeader=apiCaseRequest.getCaseapiHeader();
        JSONObject caseApiBody=apiCaseRequest.getCaseapiBody();
        JSONArray caseApiQuery=apiCaseRequest.getCaseapiQuery();
        JSONArray caseApiRelation=apiCaseRequest.getCaseapiRelation();
        JSONArray caseApiController=apiCaseRequest.getCaseapiController();
        JSONArray caseApiAssertion=apiCaseRequest.getCaseapiAssertion();
        String caseApiMock=apiCaseRequest.getCaseapiMock();

        apiCaseDTO.setCaseapiName(caseApiName);
        apiCaseDTO.setCaseapiDesc(caseApiDesc);
        apiCaseDTO.setCaseapiHeader(String.valueOf(caseApiHeader));
        apiCaseDTO.setCaseapiBody(String.valueOf(caseApiBody));
        apiCaseDTO.setCaseapiQuery(String.valueOf(caseApiQuery));
        apiCaseDTO.setCaseapiRelation(String.valueOf(caseApiRelation));
        apiCaseDTO.setCaseapiController(String.valueOf(caseApiController));
        apiCaseDTO.setCaseapiAssertion(String.valueOf(caseApiAssertion));
        apiCaseDTO.setCaseapiMock(caseApiMock);

        if(apiCaseRequest.getId() == null || apiCaseRequest.getId().equals("")){
            // 新增接口
            String uuid=UUID.randomUUID().toString();
            apiCaseDTO.setId(uuid);
            apiCaseDTO.setApiId(apiId);
            caseApiMapper.saveCaseApi(apiCaseDTO);
            return uuid;
        }else{
            // 修改接口
            apiCaseDTO.setId(apiCaseRequest.getId());
            caseApiMapper.updateCaseApi(apiCaseDTO);
            return  apiCaseRequest.getId();
        }

    }

    public ApiCaseDTO getCaseApiDetailByCaseApiId(String caseApiId){
        return caseApiMapper.getCaseApiDetailByCaseApiId(caseApiId);
    }


    public List<CaseApiReportDTO> getCaseApiReport(String projectId,String condition,Boolean existsInCaseApiIndex){
        return caseApiMapper.caseApiReport(projectId,condition,existsInCaseApiIndex);
    }


    public String copyCaseApi(String originalCaseId) {
        // 1. 获取原始用例数据
        ApiCaseDTO originalCase = caseApiMapper.getCaseApiById(originalCaseId);
        if (originalCase == null) {
            throw new RuntimeException("要复制的用例不存在，ID: " + originalCaseId);
        }

        // 2. 创建新用例请求对象
        ApiCaseDTO newCaseRequest = new ApiCaseDTO();

        // 3. 设置新用例数据
        String uuid=UUID.randomUUID().toString();
        newCaseRequest.setId(uuid);
        newCaseRequest.setApiId(originalCase.getApiId());
        newCaseRequest.setCaseapiName(originalCase.getCaseapiName()+"副本");
        newCaseRequest.setCaseapiDesc(originalCase.getCaseapiDesc());
        newCaseRequest.setCaseapiHeader(originalCase.getCaseapiHeader());
        newCaseRequest.setCaseapiBody(originalCase.getCaseapiBody());
        newCaseRequest.setCaseapiQuery(originalCase.getCaseapiQuery());
        newCaseRequest.setCaseapiRelation(originalCase.getCaseapiRelation());
        newCaseRequest.setCaseapiController(originalCase.getCaseapiController());
        newCaseRequest.setCaseapiAssertion(originalCase.getCaseapiAssertion());

        caseApiMapper.saveCaseApi(newCaseRequest);
        return  uuid;
    }


}
